package com.lc.pianostudent.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.AnswerResultBean;
import com.lc.pianostudent.conn.PostAnswerBusi;
import com.lc.pianostudent.conn.PostAnswerIndex;
import com.lc.pianostudent.fragment.MineFragment;
import com.lc.pianostudent.util.Util;
import com.lc.pianostudent.view.CommonPopupWindow;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import junit.framework.TestResult;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/27.
 */

public class AnswerResultActivity extends BaseActivity implements View.OnClickListener,CommonPopupWindow.ViewInterface{
    @BoundView(value = R.id.back_look,isClick = true) private LinearLayout back_look;
    @BoundView(value = R.id.friend_list_layout,isClick = true) private LinearLayout friend_list_layout;
    @BoundView(R.id.count) private TextView count;
    @BoundView(R.id.week_count) private TextView week_count;
    @BoundView(R.id.result_head) private CircleImageView result_head;
    private PostAnswerBusi.PostAnswerBusiInfo currentInfo;

    private CommonPopupWindow popupWindow;
    private  SharedPreferences sp ;
    private ArrayList<ArrayList<String>> selectList;
    private List<AnswerResultBean> list = new ArrayList<>();
    private List<ArrayList<String>> list1 = new ArrayList<>();
    private SharedPreferences sp1 ;
    //每日答题
    public PostAnswerBusi postAnswerBusi = new PostAnswerBusi(new AsyCallBack<PostAnswerBusi.PostAnswerBusiInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostAnswerBusi.PostAnswerBusiInfo postAnswerBusiInfo) throws Exception {
            count.setText(postAnswerBusiInfo.yes);
            week_count.setText(postAnswerBusiInfo.history);
            GlideLoader.getInstance().get("",BaseApplication.BasePreferences.readHeadImg(),result_head,R.mipmap.new_head);
            currentInfo = postAnswerBusiInfo;
            BaseApplication.BasePreferences.saveCorrect(postAnswerBusiInfo.yes);
            BaseApplication.BasePreferences.saveWrong(postAnswerBusiInfo.wrong);
            BaseApplication.BasePreferences.saveTotalGrade(postAnswerBusiInfo.yes);
            BaseApplication.BasePreferences.saveWeekGrade(postAnswerBusiInfo.history);
            //把正确错误的数组保存到本地，在答题首页得到值用于看试题回顾
            Gson gson = new Gson();
            String data = gson.toJson(postAnswerBusiInfo.answerResultList);
            sp.edit().putString("listStr", data).commit();

            try {
                ((EverydayAnswerActivity.CallBack)getAppCallBack(EverydayAnswerActivity.class)).onRefresh();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(toast);

        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_result);
        setBackTrue();

        initView();
    }

    private void initView(){
        if(getIntent().getStringExtra("type").equals("1")){
            setTitleName("初级");
        }else if(getIntent().getStringExtra("type").equals("2")){
            setTitleName("中级");
        }else if(getIntent().getStringExtra("type").equals("3")){
            setTitleName("高级");
        }

        if(getIntent().getStringExtra("from").equals("1")){

            selectList = (ArrayList<ArrayList<String>>) getIntent().getSerializableExtra("list");
//            Log.e("=====selectList",selectList.size()+"==jy");
            sp = getSharedPreferences("User", Context.MODE_PRIVATE);

            try {

//将list集合转成字符串
                SharedPreferences.Editor edit = sp.edit();
                String listStr = Util.SceneList2String(selectList);
//存储
                edit.putString("mylistStr", listStr);

                edit.commit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            postAnswerBusi.json = getIntent().getStringExtra("json");
            postAnswerBusi.user_id = BaseApplication.BasePreferences.readUID();
            postAnswerBusi.teacherid = BaseApplication.BasePreferences.readTeacherId();
            postAnswerBusi.type = getIntent().getStringExtra("type");
            postAnswerBusi.execute();
        }else {
            sp1 = getSharedPreferences("User", Context.MODE_PRIVATE);
            count.setText(BaseApplication.BasePreferences.readTotalGrade());
            week_count.setText(BaseApplication.BasePreferences.readWeekGrade());
            GlideLoader.getInstance().get("",BaseApplication.BasePreferences.readHeadImg(),result_head,R.mipmap.new_head);
        }

    }

    private void showCustomizeDialog() {
    /* @setView 装入自定义View ==> R.layout.dialog_customize
     * 由于dialog_customize.xml只放置了一个EditView，因此和图8一样
     * dialog_customize.xml可自定义更复杂的View
     */
        if (popupWindow != null && popupWindow.isShowing()) return;
        popupWindow = new CommonPopupWindow.Builder(AnswerResultActivity.this)
                .setView(R.layout.dialog_back_answer)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setBackGroundLevel(0.8f)//取值范围0.0f-1.0f 值越小越暗
                //.setAnimationStyle(R.style.popWindow_anim_style)
                .setViewOnclickListener(this)
                .create();
        popupWindow.showAtLocation(AnswerResultActivity.this.findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back_look:
                showCustomizeDialog();
                break;

            case R.id.friend_list_layout:
                startVerifyActivity(FriendListActivity.class);
                break;
        }
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.dialog_back_answer:
                TextView corrent_count = (TextView) view.findViewById(R.id.corrent_count);
                TextView wrong_count = (TextView) view.findViewById(R.id.wrong_count);
                TextView one_text = (TextView) view.findViewById(R.id.one_text);
                TextView two_text = (TextView) view.findViewById(R.id.two_text);
                TextView three_text = (TextView) view.findViewById(R.id.three_text);
                TextView four_text = (TextView) view.findViewById(R.id.four_text);
                TextView five_text = (TextView) view.findViewById(R.id.five_text);
                TextView six_text = (TextView) view.findViewById(R.id.six_text);
                TextView seven_text = (TextView) view.findViewById(R.id.seven_text);
                TextView eight_text = (TextView) view.findViewById(R.id.eight_text);
                TextView nine_text = (TextView) view.findViewById(R.id.nine_text);
                TextView ten_text = (TextView) view.findViewById(R.id.ten_text);

                ImageView one_img = (ImageView) view.findViewById(R.id.one_img);
                ImageView two_img = (ImageView) view.findViewById(R.id.two_img);
                ImageView three_img = (ImageView) view.findViewById(R.id.three_img);
                ImageView four_img = (ImageView) view.findViewById(R.id.four_img);
                ImageView five_img = (ImageView) view.findViewById(R.id.five_img);
                ImageView six_img = (ImageView) view.findViewById(R.id.six_img);
                ImageView seven_img = (ImageView) view.findViewById(R.id.seven_img);
                ImageView eight_img = (ImageView) view.findViewById(R.id.eight_img);
                ImageView nine_img = (ImageView) view.findViewById(R.id.nine_img);
                ImageView ten_img = (ImageView) view.findViewById(R.id.ten_img);
                final ImageView dismiss = view.findViewById(R.id.dismiss);
                if(getIntent().getStringExtra("from").equals("1")){
                    corrent_count.setText(currentInfo.yes);
                    wrong_count.setText(currentInfo.wrong);
                    //第一题
                    if(currentInfo.answerResultList.get(0).getContent().equals("true")){
                        one_img.setImageResource(R.mipmap.review_right);
                        one_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        one_img.setImageResource(R.mipmap.review_wrong);
                        one_text.setTextColor(Color.parseColor("#f63f22"));
                    }
                    //第二题
                    if(currentInfo.answerResultList.get(1).getContent().equals("true")){
                        two_img.setImageResource(R.mipmap.review_right);
                        two_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        two_img.setImageResource(R.mipmap.review_wrong);
                        two_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第三题
                    if(currentInfo.answerResultList.get(2).getContent().equals("true")){
                        three_img.setImageResource(R.mipmap.review_right);
                        three_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        three_img.setImageResource(R.mipmap.review_wrong);
                        three_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第四题
                    if(currentInfo.answerResultList.get(3).getContent().equals("true")){
                        four_img.setImageResource(R.mipmap.review_right);
                        four_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        four_img.setImageResource(R.mipmap.review_wrong);
                        four_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第五题
                    if(currentInfo.answerResultList.get(4).getContent().equals("true")){
                        five_img.setImageResource(R.mipmap.review_right);
                        five_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        five_img.setImageResource(R.mipmap.review_wrong);
                        five_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第六题
                    if(currentInfo.answerResultList.get(5).getContent().equals("true")){
                        six_img.setImageResource(R.mipmap.review_right);
                        six_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        six_img.setImageResource(R.mipmap.review_wrong);
                        six_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第七题
                    if(currentInfo.answerResultList.get(6).getContent().equals("true")){
                        seven_img.setImageResource(R.mipmap.review_right);
                        seven_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        seven_img.setImageResource(R.mipmap.review_wrong);
                        seven_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第八题
                    if(currentInfo.answerResultList.get(7).getContent().equals("true")){
                        eight_img.setImageResource(R.mipmap.review_right);
                        eight_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        eight_img.setImageResource(R.mipmap.review_wrong);
                        eight_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第九题
                    if(currentInfo.answerResultList.get(8).getContent().equals("true")){
                        nine_img.setImageResource(R.mipmap.review_right);
                        nine_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        nine_img.setImageResource(R.mipmap.review_wrong);
                        nine_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第九题
                    if(currentInfo.answerResultList.get(9).getContent().equals("true")){
                        ten_img.setImageResource(R.mipmap.review_right);
                        ten_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        ten_img.setImageResource(R.mipmap.review_wrong);
                        ten_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    dismiss.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            popupWindow.dismiss();
                        }
                    });

                    one_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(0).getId())
                                    .putExtra("count","1")
                                    .putStringArrayListExtra("select",selectList.get(0)));
                        }
                    });
                    two_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(1).getId())
                                    .putExtra("count","2") .putStringArrayListExtra("select",selectList.get(1)));
                        }
                    });
                    three_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(2).getId())
                                    .putExtra("count","3").putStringArrayListExtra("select",selectList.get(2)));
                        }
                    });
                    four_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(3).getId())
                                    .putExtra("count","4").putStringArrayListExtra("select",selectList.get(3)));
                        }
                    });
                    five_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(4).getId())
                                    .putExtra("count","5").putStringArrayListExtra("select",selectList.get(4)));
                        }
                    });

                    six_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(5).getId())
                                    .putExtra("count","6").putStringArrayListExtra("select",selectList.get(5)));
                        }
                    });

                    seven_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(6).getId())
                                    .putExtra("count","7").putStringArrayListExtra("select",selectList.get(6)));
                        }
                    });

                    eight_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(7).getId())
                                    .putExtra("count","8").putStringArrayListExtra("select",selectList.get(7)));
                        }
                    });

                    nine_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(8).getId())
                                    .putExtra("count","9").putStringArrayListExtra("select",selectList.get(8)));
                        }
                    });

                    ten_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",currentInfo.answerResultList.get(9).getId())
                                    .putExtra("count","10").putStringArrayListExtra("select",selectList.get(9)));
                        }
                    });
                }else {
                    corrent_count.setText(BaseApplication.BasePreferences.readCorrect());
                    wrong_count.setText(BaseApplication.BasePreferences.readWrong());

                    String data = sp1.getString("listStr", "");
                    if (!TextUtils.isEmpty(data)) {
                        //题目id集合
                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<AnswerResultBean>>() {
                        }.getType();
                        list = gson.fromJson(data, listType);
                        //我选的答案集合
                        String liststr = sp1.getString("mylistStr", "");

                        if (!TextUtils.isEmpty(liststr)) {
                            try {
                                list1 = Util.String2SceneList(liststr);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    }
                    //第一题
                    if(list.get(0).getContent().equals("true")){
                        one_img.setImageResource(R.mipmap.review_right);
                        one_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        one_img.setImageResource(R.mipmap.review_wrong);
                        one_text.setTextColor(Color.parseColor("#f63f22"));
                    }
                    //第二题
                    if(list.get(1).getContent().equals("true")){
                        two_img.setImageResource(R.mipmap.review_right);
                        two_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        two_img.setImageResource(R.mipmap.review_wrong);
                        two_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第三题
                    if(list.get(2).getContent().equals("true")){
                        three_img.setImageResource(R.mipmap.review_right);
                        three_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        three_img.setImageResource(R.mipmap.review_wrong);
                        three_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第四题
                    if(list.get(3).getContent().equals("true")){
                        four_img.setImageResource(R.mipmap.review_right);
                        four_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        four_img.setImageResource(R.mipmap.review_wrong);
                        four_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第五题
                    if(list.get(4).getContent().equals("true")){
                        five_img.setImageResource(R.mipmap.review_right);
                        five_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        five_img.setImageResource(R.mipmap.review_wrong);
                        five_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第六题
                    if(list.get(5).getContent().equals("true")){
                        six_img.setImageResource(R.mipmap.review_right);
                        six_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        six_img.setImageResource(R.mipmap.review_wrong);
                        six_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第七题
                    if(list.get(6).getContent().equals("true")){
                        seven_img.setImageResource(R.mipmap.review_right);
                        seven_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        seven_img.setImageResource(R.mipmap.review_wrong);
                        seven_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第八题
                    if(list.get(7).getContent().equals("true")){
                        eight_img.setImageResource(R.mipmap.review_right);
                        eight_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        eight_img.setImageResource(R.mipmap.review_wrong);
                        eight_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第九题
                    if(list.get(8).getContent().equals("true")){
                        nine_img.setImageResource(R.mipmap.review_right);
                        nine_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        nine_img.setImageResource(R.mipmap.review_wrong);
                        nine_text.setTextColor(Color.parseColor("#f63f22"));
                    }

                    //第九题
                    if(list.get(9).getContent().equals("true")){
                        ten_img.setImageResource(R.mipmap.review_right);
                        ten_text.setTextColor(Color.parseColor("#02c422"));
                    }else {
                        ten_img.setImageResource(R.mipmap.review_wrong);
                        ten_text.setTextColor(Color.parseColor("#f63f22"));
                    }


                    one_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(0).getId()).putExtra("count","1")
                                    .putStringArrayListExtra("select",list1.get(0)));
                        }
                    });
                    two_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(1).getId()).putExtra("count","2")
                                    .putStringArrayListExtra("select",list1.get(1)));
                        }
                    });
                    three_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(2).getId()).putExtra("count","3")
                                    .putStringArrayListExtra("select",list1.get(2)));
                        }
                    });
                    four_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(3).getId()).putExtra("count","4")
                                    .putStringArrayListExtra("select",list1.get(3)));
                        }
                    });
                    five_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(4).getId()).putExtra("count","5")
                                    .putStringArrayListExtra("select",list1.get(4)));
                        }
                    });

                    six_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(5).getId()).putExtra("count","6")
                                    .putStringArrayListExtra("select",list1.get(5)));
                        }
                    });

                    seven_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(6).getId()).putExtra("count","7")
                                    .putStringArrayListExtra("select",list1.get(6)));
                        }
                    });

                    eight_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(7).getId()).putExtra("count","8")
                                    .putStringArrayListExtra("select",list1.get(7)));
                        }
                    });

                    nine_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(8).getId()).putExtra("count","9")
                                    .putStringArrayListExtra("select",list1.get(8)));
                        }
                    });

                    ten_img.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            startActivity(new Intent(AnswerResultActivity.this,AnswerBackActivity.class)
                                    .putExtra("id",list.get(9).getId()).putExtra("count","10")
                                    .putStringArrayListExtra("select",list1.get(9)));
                        }
                    });
                    dismiss.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            popupWindow.dismiss();
                        }
                    });
                }


                break;
        }
    }
}
